﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Practices.Composite.Presentation.Commands;
using AuthenticationService.Client.Controls;

namespace UserManager.Module.CommandBehaviors
{
    public static class LoginCommand
    {
        private static readonly DependencyProperty LoginCommandBehaviorProperty
            = DependencyProperty.RegisterAttached(
            "LoginCommandBehavior",
            typeof(LoginViewCommandBehavior),
            typeof(LoginCommand),
            null);

        public static readonly DependencyProperty CommandProperty
            = DependencyProperty.RegisterAttached(
            "Command",
            typeof(ICommand),
            typeof(LoginCommand),
            new PropertyMetadata(OnSetCommandCallback));

        public static readonly DependencyProperty CommandParameterProperty
            = DependencyProperty.RegisterAttached(
           "CommandParameter",
           typeof(object),
           typeof(LoginCommand),
           new PropertyMetadata(OnSetCommandParameterCallback));

        public static ICommand GetCommand(Control control)
        {
            return control.GetValue(CommandProperty) as ICommand;
        }

        public static void SetCommand(Control control, ICommand command)
        {
            control.SetValue(CommandProperty, command);
        }

        public static void SetCommandParameter(Control control, object parameter)
        {
            control.SetValue(CommandParameterProperty, parameter);
        }

        public static object GetCommandParameter(Control control)
        {
            return control.GetValue(CommandParameterProperty);
        }

        private static void OnSetCommandCallback
            (DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
        {
            LoginControl control = dependencyObject as LoginControl;
            if (control != null)
            {
                LoginViewCommandBehavior behavior = GetOrCreateBehavior(control);
                behavior.Command = e.NewValue as ICommand;
            }
        }

        private static void OnSetCommandParameterCallback
            (DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
        {
            LoginControl control = dependencyObject as LoginControl;
            if (control != null)
            {
                LoginViewCommandBehavior behavior = GetOrCreateBehavior(control);
                behavior.CommandParameter = e.NewValue;
            }
        }

        private static LoginViewCommandBehavior GetOrCreateBehavior(LoginControl control)
        {
            LoginViewCommandBehavior behavior =
                control.GetValue(LoginCommandBehaviorProperty) as LoginViewCommandBehavior;
            if (behavior == null)
            {
                behavior = new LoginViewCommandBehavior(control);
                control.SetValue(LoginCommandBehaviorProperty, behavior);
            }
            return behavior;
        }
    }

    public class LoginViewCommandBehavior : CommandBehaviorBase<LoginControl>
    {
        public LoginViewCommandBehavior(LoginControl control)
            : base(control)
        {
            control.Login += OnLogin;
        }

        private void OnLogin(object sender, RoutedEventArgs e)
        {
            ExecuteCommand();
        }
    }
}
